package com.joeyee.echartsgenerator.service;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.echarts.json.GsonOption;
import com.joeyee.echartsgenerator.echarts.EchartData;
import com.joeyee.echartsgenerator.echarts.bar.BarData;
import com.joeyee.echartsgenerator.echarts.bar.EchartBar;
import com.joeyee.echartsgenerator.echarts.line.EchartBaseLine;
import com.joeyee.echartsgenerator.echarts.line.LineData;
import com.joeyee.echartsgenerator.echarts.map.EchartMap;
import com.joeyee.echartsgenerator.echarts.map.MapData;
import com.joeyee.echartsgenerator.echarts.wordcloud.EchartWordCloud;
import com.joeyee.echartsgenerator.echarts.wordcloud.WordCloudData;
import com.joeyee.echartsgenerator.enums.EchartsType;
import com.joeyee.echartsgenerator.utils.EchartsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Base64;

/**
 * @author Joe Yee
 * @version 1.0
 * @description: 后台生成Echarts图片的Service类
 * @date 2021/8/9 18:13
 */
@Slf4j
@Service
public class EchartService {
    /**
     * @description:  成功截取图片后，存放的图片路径
     * @author Joe Yee
     * @date: 2021/8/9 18:26
     */
    @Value("${img-url}")
    private String imgUrl;

    /** 
     * @description: 请求phantomjs服务端地址
     * @author Joe Yee
     * @date: 2021/8/9 18:31
     */ 
    @Value("${request-url}")
    private String requestUrl;

    public byte[] buildEchart(EchartsType type, EchartData echartData){
        /**
         * @description: 生成Echarts图表所需的json字符串，通过工具类EchartBar统一生成柱状图json字符串；
         * @author Joe Yee
         * @date: 2021/8/9 18:10
         */
        GsonOption option = null;
        switch (type){
            case 条形图:
                option = EchartBar.createBar((BarData) echartData);
                break;
            case 折线图:
                option = EchartBaseLine.createLine((LineData)echartData);
                break;
            case 中国地图:
                option = EchartMap.createMap((MapData)echartData);
                break;
            case 词云图:
                option = EchartWordCloud.createWordCloud((WordCloudData)echartData);
                break;
            default:
                log.info("没有合适的图形可以生成");
                break;
        }

        /**
         * @description:  将生成Echarts的配置项转化为字符串
         * @author Joe Yee
         * @date: 2021/8/10 14:22
         */
        String optionStr = JSONObject.toJSONString(option);

//        optionStr = "{'series': [{    'type': 'wordCloud',    'gridSize': 2,    'sizeRange': [10, 60],    'rotationRange': [0, 0],    'shape': 'triangle',    'textStyle': { 'normal': { color: '#b11c25' } },    'data': [        { 'name': 'aaaa', 'value': 10000 },        { 'name': 'bbbb', 'value': 6181 },        { 'name': 'cccc', 'value': 4386 },        { 'name': 'dddd', 'value': 4055 },        { 'name': 'eeee', 'value': 2467 },        { 'name': 'ffff', 'value': 2244 },        { 'name': 'gggg', 'value': 1898 },        { 'name': 'hhhh', 'value': 1484 },        { 'name': 'iiii', 'value': 1112 },        { 'name': 'jjjj', 'value': 965 },        { 'name': 'kkkk', 'value': 847 },        { 'name': 'llll', 'value': 582 },        { 'name': 'mmmm', 'value': 555 },        { 'name': 'nnnn', 'value': 550 },        { 'name': 'oooo', 'value': 462 },        { 'name': 'pppp', 'value': 366 },        { 'name': 'qqqq', 'value': 360 },        { 'name': 'rrrr', 'value': 282 },        { 'name': 'ssss', 'value': 273 },        { 'name': 'tttt', 'value': 265 }    ]}]}";

        if(optionStr == null || "".equals(optionStr)){
            return null;
        }
        log.info("optionStr = " + optionStr);

        try {
            // 根据option参数发起请求，转换为base64
            String base64 =  EchartsUtil.generateEchartsBase64(optionStr, requestUrl);
            Base64.Decoder decoder = Base64.getDecoder();
            return decoder.decode(base64);
        }catch (Exception e){
            log.error("发生了异常，" + e.getMessage());
            return null;
        }
    }
}
